這篇RecyclerView與前幾篇介紹ListView相似,主要是用清單條列出來,讓使用者簡單明瞭,
RecyclerView是Google在Android5.0時推出,目的是優化ListView相關程式及效能。
今天這篇利用RecyclerView來做一個範例
要利用RecyclerView要先在Gradle Scripts/build.grade(.app)裡的dependencies新增
implementation 'androidx.recyclerview:recyclerview:1.1.0'
接著在activity_main.xml裡新增recyclerView元件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycleView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
接著新增recyclerView要顯示的排版xml檔
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/myImageView"
android:layout_width="100dp"
android:layout_height="100dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_launcher_background"
tools:ignore="VectorDrawableCompat" />
<TextView
android:id="@+id/myText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="16dp"
android:text="Title"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="@+id/myImageView"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/myText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:text="Description"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/myImageView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.813" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
排版設定好可以透過values/strings來設定要顯示的文字
<resources>
<string name="app_name">Recycleview</string>
<string-array name="progrmming_languages">
<item>可麗露</item>
<item>舒芙蕾</item>
<item>瑪德蓮</item>
<item>馬卡龍</item>
<item>泡芙</item>
</string-array>
<string-array name="description">
<item>是一種小型的法式甜點,表層則是硬脆\n又厚實的褐色焦糖外殼,內部是半融化\n狀的蛋糕糊,散發著酒香和香草味。</item>
<item>是一種源自法國的甜品,經烘焙後質感\n輕而蓬鬆</item>
<item>是一種傳統的貝殼形狀的小蛋糕,來自\n於法國東北部洛林大區的兩個市鎮科梅\n爾西和利韋爾丹</item>
<item>是一種用色彩繽紛絢麗的法國甜品,外\n殼堅硬但易碎,內陷黏稠扎實</item>
<item>是一種源自法國的球形糕點,蓬鬆張孔\n的麵皮中包裹鮮奶油、巧克力乃至冰淇\n淋</item>
</string-array>
</resources>
接著建立Adapter來控管每個item的顯示設定,我們自訂了一個名為MyAdapter的類別,此類別繼承了 RecyclerView.Adapter
package com.example.recycleview;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
String data1[],data2[];
int images[];
Context context;
public MyAdapter(Context ct,String s1[],String s2[],int img[]){
context = ct;
data1 = s1;
data2 = s2;
images = img;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.my_row,parent,false);
return new MyViewHolder(view);
}
@Override
//透過 position 參數指定每個item所用到的資料
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.myText1.setText(data1[position]);
holder.myText2.setText(data2[position]);
holder.myImage.setImageResource(images[position]);
}
@Override
public int getItemCount() {
return images.length;
}
public class MyViewHolder extends RecyclerView.ViewHolder{
TextView myText1,myText2;
ImageView myImage;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
myText1 = itemView.findViewById(R.id.myText1);
myText2 = itemView.findViewById(R.id.myText2);
myImage = itemView.findViewById(R.id.myImageView);
}
}
}
設定完Adapter後就可以在MainActivity.java中設定RecyclerView
package com.example.recycleview;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
RecyclerView recycleView;
String s1[] ,s2[];
int images[] = {R.drawable.coco,R.drawable.su,R.drawable.lan,R.drawable.mar,R.drawable.pabu};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recycleView = findViewById(R.id.recycleView);
s1 = getResources().getStringArray(R.array.progrmming_languages);
s2 = getResources().getStringArray(R.array.description);
MyAdapter myAdapter = new MyAdapter(this,s1,s2,images);
recycleView.setAdapter(myAdapter);
recycleView.setLayoutManager(new LinearLayoutManager(this));
}
}
這樣就完成囉